package mi.payment.ui.admin;

import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.CompoundButton;
import android.widget.TextView;

import mi.payment.R;
import mi.payment.settings.OfflineTransactionSettings;
import mi.payment.settings.OfflineTransactionOperation;
import mi.payment.ui.base.ParentActivity;
import sunmi.sunmiui.button.ButtonSwitch;
import sunmi.sunmiui.dialog.DialogCreater;
import sunmi.sunmiui.dialog.EditTextDialog;
import sunmi.sunmiui.title.Title;

/**
 * 离线交易控制
 * Created by lj on 2017/2/15.
 */
public class OfflineTransactionSettingActivity extends ParentActivity {
    private View offlinePushNumberView, automaticSendingCountView, supportSmallPaymentWayView;
    private ButtonSwitch mSwitch;
    private EditTextDialog mEditTextDialog;
    private TextView offlinePushNumberRightText,automaticSendingCountRightText;
    private OfflineTransactionOperation mOfflineTransactionOperation;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_offline_transaction_setting);
        mOfflineTransactionOperation = OfflineTransactionOperation.getInstance();
        initView();
        setListener();
        updateUI();
    }

    /**
     * 初始化控件
     */
    private void initView() {
        Title mTitle;
        TextView leftText;
        mTitle = (Title) findViewById(R.id.offline_transaction_setting_title);
        mTitle.setButtonVisibility(false, true, true, true);
        mTitle.setHeadBgColor(getResources().getColor(R.color.colorBar));
        mTitle.setTitleText(getResources().getString(R.string.off_line_transaction_setting));
        mTitle.setMenuClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OfflineTransactionSettingActivity.this.finish();
            }
        }, null, null, null);

        offlinePushNumberView = findViewById(R.id.offline_push_number);
        automaticSendingCountView = findViewById(R.id.automatic_sending_count);
        supportSmallPaymentWayView = findViewById(R.id.support_small_payment_way);
        mSwitch = (ButtonSwitch) supportSmallPaymentWayView.findViewById(R.id.set_switch);

        leftText = (TextView) offlinePushNumberView.findViewById(R.id.item_left_text);
        leftText.setText(getResources().getString(R.string.offline_push_number1));

        leftText = (TextView) automaticSendingCountView.findViewById(R.id.item_left_text);
        leftText.setText(getResources().getString(R.string.automatic_sending_count1));

        leftText = (TextView) supportSmallPaymentWayView.findViewById(R.id.switch_item_text);
        leftText.setText(getResources().getString(R.string.support_small_payment_way));

        offlinePushNumberRightText = (TextView) offlinePushNumberView.findViewById(R.id.item_right_text);
        offlinePushNumberRightText.setText(getResources().getString(R.string.offline_push_number2));

        automaticSendingCountRightText = (TextView) automaticSendingCountView.findViewById(R.id.item_right_text);
        automaticSendingCountRightText.setText(getResources().getString(R.string.automatic_sending_count2));
    }

    private void setListener() {
        offlinePushNumberView.setOnClickListener(this);
        automaticSendingCountView.setOnClickListener(this);
        supportSmallPaymentWayView.setOnClickListener(this);
        mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mOfflineTransactionOperation.saveSmallPaymentWay(isChecked);
                updateUI();
            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.offline_push_number:
                //离线上送次数
                mEditTextDialog = DialogCreater.createEditTextDialog(this, getString(R.string.cancel),
                        getString(R.string.save), getString(R.string.offline_push_number1), new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                mEditTextDialog.dismiss();
                            }
                        }, new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if(checkEmpty(mEditTextDialog.getText())){
                                    if(Integer.parseInt(mEditTextDialog.getText())==0){
                                        showToast(getString(R.string.error_input));
                                    }else{
                                        mOfflineTransactionOperation.saveOfflinePushCount(Integer.parseInt(mEditTextDialog.getText()));
                                        mEditTextDialog.dismiss();
                                        updateUI();
                                    }
                                }
                            }
                        }, new DialogInterface.OnShowListener() {
                            @Override
                            public void onShow(DialogInterface dialog) {
                                mEditTextDialog.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
                                mEditTextDialog.setMaxLength(1);
                                showSoftKeyboard(mEditTextDialog);
                            }
                        });
                if(TextUtils.isEmpty(mOfflineTransactionOperation.getOfflineTransactionSettings().getOfflinePushCount()+"")){
                    mEditTextDialog.setHintText(getResources().getString(R.string.not_filled));
                }else{
                    mEditTextDialog.setText(String.valueOf(mOfflineTransactionOperation.getOfflineTransactionSettings().getOfflinePushCount()));
                }
                mEditTextDialog.show();

                break;
            case R.id.automatic_sending_count:
                //自动上送的累计笔数
                mEditTextDialog = DialogCreater.createEditTextDialog(this, getString(R.string.cancel),
                        getString(R.string.save), getString(R.string.automatic_sending_count1), new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                mEditTextDialog.dismiss();
                            }
                        }, new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if(checkEmpty(mEditTextDialog.getText())) {
                                    mOfflineTransactionOperation.saveAutomaticallyNumber(Integer.parseInt(mEditTextDialog.getText()));
                                    mEditTextDialog.dismiss();
                                    updateUI();
                                }
                            }
                        }, new DialogInterface.OnShowListener() {
                            @Override
                            public void onShow(DialogInterface dialog) {
                                mEditTextDialog.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
                                showSoftKeyboard(mEditTextDialog);
                                mEditTextDialog.setMaxLength(2);
                            }
                        });
                if(TextUtils.isEmpty(mOfflineTransactionOperation.getOfflineTransactionSettings().getAutomaticallyNumber()+"")){
                    mEditTextDialog.setHintText(getResources().getString(R.string.not_filled));
                }else{
                    mEditTextDialog.setText(mOfflineTransactionOperation.getOfflineTransactionSettings().getAutomaticallyNumber()+"");
                }
                mEditTextDialog.show();

                break;
            case R.id.support_small_payment_way:
                //是否支持小额代付方式
                mSwitch.setOnCheck(!mSwitch.isChecked());
                break;
        }
    }

    private void updateUI(){
        OfflineTransactionSettings offlineTransactionSettings = mOfflineTransactionOperation.getOfflineTransactionSettings();
        if(offlineTransactionSettings.getOfflinePushCount() <= 0 ){
            offlinePushNumberRightText.setText(getResources().getString(R.string.offline_push_number2));
        }else{
            offlinePushNumberRightText.setText(String.valueOf(offlineTransactionSettings.getOfflinePushCount()));
        }

        if(offlineTransactionSettings.getAutomaticallyNumber() <= 0 ){
            automaticSendingCountRightText.setText(getResources().getString(R.string.automatic_sending_count2));
        }else{
            automaticSendingCountRightText.setText(String.valueOf(offlineTransactionSettings.getAutomaticallyNumber()));
        }

        mSwitch.setOnCheck(offlineTransactionSettings.isSmallPaymentWay());
    }
}
